/**
 * @author 黄波
 * 创建日期：2011Oct 11, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.os.util.Mail;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 重置密碼
 */
public class ResetPasswdAction extends NJCCSuport {

	private String orginPasswd;
	private String newPasswd;
	
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		HttpSession session = ServletActionContext.getRequest().getSession();
		final UserInfo user = (UserInfo) session.getAttribute("user");
		if(user == null || user.getId() == null || user.getId().intValue() == 0) {
			out.flush();
			out.write("<script>alert('请先登录！');history.back();</script>");
			out.close();
			return null;
		}
		if(orginPasswd == null || orginPasswd.trim().equals("") || newPasswd == null || newPasswd.trim().equals("")) {
			out.flush();
			out.write("<script>alert('表单填写不完整！');history.back();</script>");
			out.close();
			return null;
		}
		//查看当前用户密码与所填原密码是否相同
		if(!user.getLoginPasswd().equals(orginPasswd)) {
			out.flush();
			out.write("<script>alert('原始密码填写错误！');history.back();</script>");
			out.close();
			return null;
		}
		user.setLoginPasswd(newPasswd);
		//进行数据更新并发送邮件
//		new Thread() {
//			public void run() {
//				//加载系统配置文件
//				try {
//					String systemProfile = "/system.properties";
//					InputStream stream = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/") + systemProfile));
//					Properties properties = new Properties();
//					properties.load(stream);
//					stream.close();
//					Mail.sendMail("密码修改成功-南京化工职业技术学院资源共享平台！","<br/>您的新密码为：" + user.getLoginPasswd() + "，<a href='" + properties.getProperty("server_static_addr") + "' style='color:red;'>点击这里，进行访问</a>！<br/><br/>---------------<br/>南京化工职业技术学院--资源共享平台<br/>", user.getEmail());
//				} catch (FileNotFoundException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}.start();
		UserBo.getInstance().updateUser(user);
		out.flush();
		out.write("<script>alert('修改成功！');history.back();</script>");
		out.close();
		return null;
	}
	
	public String getOrginPasswd() {
		return orginPasswd;
	}
	public void setOrginPasswd(String orginPasswd) {
		this.orginPasswd = orginPasswd;
	}
	public String getNewPasswd() {
		return newPasswd;
	}
	public void setNewPasswd(String newPasswd) {
		this.newPasswd = newPasswd;
	}
	
}
